package com.warski.wonderfires.page.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.warski.wonderfires.R;
import com.warski.wonderfires.dlna.DLNAManager;
import com.warski.wonderfires.dlna.listener.DLANDeviceConnectListener;
import com.warski.wonderfires.dlna.listener.DLNADeviceListener;
import com.warski.wonderfires.dlna.model.DeviceInfo;
import com.warski.wonderfires.media.MusicPlayer;
import com.warski.wonderfires.view.TitleBarView;

import java.util.List;

public class SearchDeviceFragment extends Fragment {
    private SearchDeviceAdaopter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_search_device, container, false);

        initTitleBar(root);
        startSearchDevice(root);

        return root;
    }

    @Override
    public void onDestroyView() {
        DLNAManager.stopSearch();
        DLNAManager.removeDeviceListener();
        super.onDestroyView();
    }

    private void initTitleBar(View root) {
        final TitleBarView titleBarView = root.findViewById(R.id.titlebar);
        titleBarView.setLeftIconOnClickListener(v -> NavHostFragment
                .findNavController(SearchDeviceFragment.this)
                .navigate(R.id.action_SearchDeviceFragment_to_MusicFragment));
    }

    private void startSearchDevice(final View root) {
        final ListView listView = root.findViewById(R.id.devicelist);
        adapter = new SearchDeviceAdaopter(root.getContext());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            final DeviceInfo deviceInfo = adapter.getItem(position);
            if (deviceInfo.isConnected()) {
                new AlertDialog.Builder(root.getContext())
                        .setTitle(R.string.disconnect_device_title)
                        .setMessage(R.string.disconnect_device_message)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DLNAManager.disconnectDevice();
                                MusicPlayer.stopDlna();
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton(R.string.no, null)
                        .show();
            } else {
                new AlertDialog.Builder(root.getContext())
                        .setTitle(R.string.connect_device_title)
                        .setMessage(R.string.connect_device_message)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DLNAManager.connectDevice(deviceInfo, new DLANDeviceConnectListener() {
                                    public void onConnected() {
                                        MusicPlayer.startDlna();
                                        adapter.notifyDataSetChanged();
                                        NavHostFragment
                                                .findNavController(SearchDeviceFragment.this)
                                                .navigate(R.id.action_SearchDeviceFragment_to_MusicFragment);
                                    }
                                });
                            }
                        })
                        .setNegativeButton(R.string.no, null)
                        .show();
            }
        });

        DLNAManager.setDeviceListener(new DLNADeviceListener() {
            @Override
            public void onDeviceListRefresh(List<DeviceInfo> deviceList) {
                if (deviceList.isEmpty()){
                    Toast.makeText(getContext(),R.string.no_search_device,Toast.LENGTH_SHORT).show();
                }
                adapter.clear();
                adapter.addAll(deviceList);
                adapter.notifyDataSetChanged();
            }
        });
        DLNAManager.startSearch();
    }


}
